 <script>
      // 1.替换字符串中的第一相同字符
      let str = "anadasllsajdasf";
      str = str.replace("a", "");

      // 替换所有相同字符
      // str = str.replace(/\a/g, "1");
      // str = str.replaceAll("a", "2");
      console.log(str);

      // 2.获取字符串中的某个字符和查找是否存在某个字符
      console.log(str.charAt(0));

      // var str1 = "helloWorld";
      str.includes("e", 1);

      // 3.反转字符串
      let str1 = "hello world";
      const stringReverse = (str) => str1.split("").reverse().join("");
      let newStr = stringReverse("hello world");
      console.log(newStr);

      //4.数字型字符串转成number类型
      let num = "12";
      +num; //12
      num * 1; //12
      // 优雅的取整
      let number = 2.345 | 0;
      console.log(number);

      // 5.条件判断给字符变量赋值 也叫短路评估
      let str8 = "";
      let finalStr;
      // Longhand
      if (str8 !== null && str8 !== undefined && str8 != "") {
        finalStr = str8;
      } else {
        finalStr = "default string";
      }
      // Shorthand
      finalStr = str8 || "default string"; // 'default string'
      console.log(finalStr);

      // 6.通过条件判断增加对象属性
      let isAdd = true;
      let girlfriend = {
        name: "Lucy",
        height: "176cm",
        age: 23,
        ...(isAdd && { work: "singer" }),
      };

      // 7.检查某个属性是否存在对象中
      console.log(!girlfriend.work);

      console.log("work" in girlfriend);

      console.log(girlfriend.hasOwnProperty("age"));

      console.log(Object.keys(girlfriend).indexOf("age"));

      // 8.标准JSON的深拷贝
      var a = {
        a: 1,
        b: {
          c: 2,
          d: 8,
        },
      };

      var b = JSON.parse(JSON.stringify(a));
      // var b = a;
      b.a = 233;
      console.log(b);
      console.log(a);

      // 9.快速遍历数组
      // const fruits = ["mango", "peach", "banana"];
      const fruits = [
        {name:"mango",price:12}
      ]
      for (let item of fruits) {
        console.log(item.price);
        /* something */
      }

      // 10.统计一个数组中每个元素出现的次数
      let arr2 = [1, 2, 3, 4, 2, 3, 8, 9];

      arr2.reduce((a, b) => {
        console.log((a[b] = (a[b] || 0) + 1));
        return a;
      }, {});
      //{1: 1, 2: 2, 3: 2, 4: 1, 8: 1, 9: 1}

      var numArr = numbers.reduce(function (a, b) {
        if (b in a) {
          a[b]++;
        } else {
          a[b] = 1;
        }
        return a;
      }, {});
      console.log(numArr);
    </script>
  </body>
</html>
